﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OptimumInfra.ViewData;
namespace OptimumInfra.Models
{
    public class Repository
    {
        private OptimumDataContext op = new OptimumDataContext();

        public void CreateCustomer(Customer c)
        {
            op.Customers.InsertOnSubmit(c);
            op.SubmitChanges();
        }

        #region resource
        public IEnumerable<ViewDataResource> GetAllResource()
        {
            var result = from r in op.Resources
                         select r;

            List<ViewDataResource> t = new List<ViewDataResource>();
            foreach (var r in result)
            {
                ViewDataResource temp = new ViewDataResource();
               // temp.AmountList = AmountResourceList((int)r.Id);
                temp.Description = r.Description;
                temp.Name = r.Name;
                temp.Unit = r.Unit;
                temp.Id = r.Id;
                temp.PricePerAmount = ResourcePricePerAmount((int)r.Id);
                t.Add(temp);
            }
            return t;
        }


        public Dictionary<int, string> PricePerAmount(int itemId)
        {
            Dictionary<int, string> t = new Dictionary<int, string>();
            var result = from r in op.SupportServices
                         join a in op.Attributes on r.Id equals a.ResourceId
                         where r.Id == itemId
                         select new { a.Id, a.Amount, a.Price };
            foreach (var r in result)
            {
                string price = r.Price.ToString() + "_" + r.Id.ToString();
                t.Add((int)r.Amount, price);
            }
            return t;
        }
        public void CreateResource(Resource r)
        {
            op.Resources.InsertOnSubmit(r);
            op.SubmitChanges();
        }
        public bool IsAttributeExisted(int type, int typeId, int amount )
        {
            if (type == 1)
            {
                return this.op.Attributes.Any(r => r.Amount == amount && r.ResourceId == typeId);
            }
            else
            {
                return this.op.Attributes.Any(r => r.Amount == amount && r.SupportId == typeId);
            }
        }
        public string CreateResourceAttribute(int resourceId, int amount, int price)
        {
            Attribute attribute = new OptimumInfra.Models.Attribute();
            attribute.Amount = amount;
            attribute.ResourceId = resourceId;
            attribute.Price = price;
            if (!IsAttributeExisted(1, resourceId, amount))
            {
                op.Attributes.InsertOnSubmit(attribute);
                op.SubmitChanges();
                return "Attribute created successfully";
            }
            else
            {
                return "Please enter different amount";
            }
        }

        public void UpdateResource(Resource r)
        {
            op.SubmitChanges();
        }

        public ViewDataResource GetResourceById(int id)
        {
            var result = op.Resources.Single(a => a.Id == id);
            ViewDataResource resource = new ViewDataResource();
            resource.Id = result.Id;
            resource.Name = result.Name;
            resource.Description = result.Description;
            resource.Unit = result.Unit;
            resource.PricePerAmount = ResourcePricePerAmount(id);
            return resource;

        }
        public Dictionary<int, string> ResourcePricePerAmount(int itemId)
        {
            Dictionary<int, string> t = new Dictionary<int, string>();
            var result = from r in op.Resources
                         join a in op.Attributes on r.Id equals a.ResourceId
                         where r.Id == itemId
                         select new { a.Id, a.Amount, a.Price };
            foreach (var r in result)
            {
                string price = r.Price.ToString() + "_" + r.Id.ToString();
                t.Add((int)r.Amount, price);
            }
            return t;
        }

        public void DeleteResourceById(Resource r)
        {

            op.Resources.DeleteOnSubmit(r);
            op.SubmitChanges();
        }

        public void UpdateResource(SupportService r)
        {
            op.SubmitChanges();
        }

        #endregion
        #region service
        public void CreateServiceAttribute(int supportId, int amount, int price)
        {
            Attribute attribute = new OptimumInfra.Models.Attribute();
            attribute.Amount = amount;
            attribute.SupportId = supportId;
            attribute.Price = price;
            if (!IsAttributeExisted(2, supportId, amount))
            {
                op.Attributes.InsertOnSubmit(attribute);
                op.SubmitChanges();
            }
        }

        public IEnumerable<ViewDataSupportServices> GetAllServices()
        {
            var result = from r in op.SupportServices
                         select new { r.Id, r.Item, r.Description, r.Unit };

            List<ViewDataSupportServices> t = new List<ViewDataSupportServices>();
            foreach (var r in result)
            {
                ViewDataSupportServices temp = new ViewDataSupportServices();
                temp.Description = r.Description;
                temp.Unit = r.Unit;
                //temp.PricePerAmount = ServicePricePerAmount((int)r.Id);
                temp.Name = r.Item;
                temp.Id = r.Id;
                t.Add(temp);

            }
            return t;
        }

        public Dictionary<int, int> ServicePricePerAmount(int itemId)
        {
            Dictionary<int, int> t = new Dictionary<int, int>();
            var result = from r in op.SupportServices
                         join a in op.Attributes on r.Id equals a.SupportId

                         where r.Id == itemId
                         select new { a.Amount, a.Price };
            foreach (var r in result)
            {
                t.Add((int)r.Amount, (int)r.Price);
            }
            return t;
        }

        public void CreateService(SupportService r)
        {
            op.SupportServices.InsertOnSubmit(r);
            op.SubmitChanges();
        }
       

        public ViewDataSupportServices GetServiceById(int id)
        {
            var result = op.SupportServices.SingleOrDefault(a => a.Id == id);
            ViewDataSupportServices resource = new ViewDataSupportServices();
            resource.Id = result.Id;
            resource.Name = result.Item;
            resource.Description = result.Description;
            resource.Unit = result.Unit;
          //  resource.PricePerAmount = ServicePricePerAmount(id);
            return resource;
        }
        public void DeleteResourceById(SupportService r)
        {
            op.SupportServices.DeleteOnSubmit(r);
            op.SubmitChanges();
        }
        #endregion

        #region template
        public IEnumerable<ServerTemplate> GetAllTemplates()
        {
            return (from a in op.ServerTemplates select a).ToList();
        }
        public ServerTemplate GetTemplateById(int id)
        {
            return op.ServerTemplates.SingleOrDefault(a => a.Id == id);
        }
        public IEnumerable<ServerTemplate> GetTemplatesByCategory(int id)
        {
           return (from a in op.ServerTemplates.Where(a=>a.CategoryId == id) select a)
                    .Take(15).ToList();
        }
        public void CreateTemplate(ServerTemplate r)
        {
            op.ServerTemplates.InsertOnSubmit(r);
            op.SubmitChanges();
        }
        public void UpdateTemplate(ServerTemplate r)
        {
            op.SubmitChanges();
        }

        public void DeleteTemplateById(ServerTemplate r)
        {
            op.ServerTemplates.DeleteOnSubmit(r);
            op.SubmitChanges();
        }

        #endregion

        #region templatecategory

        public List<TemplateCategory> GetAllCategories()
        {
            return (from a in op.TemplateCategories select a).ToList();
        }


        public TemplateCategory GetCategoryById(int id)
        {
            return op.TemplateCategories.SingleOrDefault(a => a.Id == id);
        }

        public void CreateCategory(TemplateCategory r)
        {
            op.TemplateCategories.InsertOnSubmit(r);
            op.SubmitChanges();
        }
        public void UpdateTemplate(TemplateCategory r)
        {
            op.SubmitChanges();
        }

        public void DeleteCategoryById(TemplateCategory r)
        {
            op.TemplateCategories.DeleteOnSubmit(r);
            op.SubmitChanges();
        }

        #endregion

        #region attribute
        public void DeleteAttributeById(Attribute r)
        {

            op.Attributes.DeleteOnSubmit(r);
            op.SubmitChanges();
        }
        public Attribute GetAttributeById(int id)
        {

            return op.Attributes.Single(a => a.Id == id);
        }

        public Attribute GetResourceAttributeByAmount(int resourceId, int amount)
        {

            var r = from a in op.Attributes where a.Amount == amount select a;
            Attribute attribute = new Attribute();
            foreach (Attribute a in r)
            {
                attribute = a;
                break;
            }
            return attribute;

        }
        #endregion
    }
}